module TimeGroup
  def TimeGroup.get_key group_by, time = Time.now
    case group_by
      when 'day' then 
        [time.year, time.strftime('%m'), time.strftime('%d')].join('-')
      when 'week' then
        week = time.strftime('%V').to_i
        week_day = time.strftime('%u').to_i
        first_day = (time - (week_day - 1).days).strftime('%Y/%m/%d')
        last_day = (time + (7 - week_day).days).strftime('%Y/%m/%d')
        [time.strftime('%G'), week].join('-') + '(' + [first_day, last_day].join('-') + ')'
      when 'month' then 
        [time.year, time.strftime('%m')].join('-')
      else
        ''
    end
  end
end